package com.seio.edc.controller.system;

import com.alibaba.fastjson.JSON;
import com.nova.edc.model.system.*;
import com.nova.edc.service.system.*;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by leshare on 2015-12-09.
 */
@Controller
@RequestMapping(value = "/systemCollectAreaAir")
public class CollectAreaAirContronller {
    @Autowired
    private CollectAreaService novaCollectAreaService;

    @Autowired
    private DeviceTypeService deviceTypeService;

    @Autowired
    private CollectAreaItemAirService collectAreaItemAirService;

    @Autowired
    private CustomerInfoService novaCustomerInfoService;

    @Autowired
    private DeviceAirService deviceAirService;

    private static final int PAGE_SIZE = 10;

    @RequestMapping(value = "collectAreaAirList")
    public ModelAndView collectAreaAirList(HttpServletRequest request, HttpSession session, Integer pageIndex,
                                           CollectArea collectArea, ModelMap modelMap) {
        Login login = (Login) session.getAttribute("sessionLogin");
        collectArea.setAddby(login.getUsername());
        // 当前页数
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        novaCollectAreaService.trimCollectArea(collectArea);
        //获取区域的设备类型
        DeviceType deviceType = deviceTypeService.getByName(ModelConstant.DEVICE_AIR);
        collectArea.setDevicetype(deviceType.getId());
        PageList<CollectArea> pageList = novaCollectAreaService
                .findPageCollectArea(collectArea, page,
                        "GatewayID,SensorID,ChannelID", "asc");
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("collectArea", collectArea);
        modelMap.put("pageCount", page.getTotalPage());
        // 数据列表
        modelMap.put("list", pageList.getDatas());
        return new ModelAndView("system/collectAreaAirList", modelMap);
    }


    /**
     * 添加初始化
     */
    @RequestMapping(value = "collectAreaAirEdit.shtml")
    public ModelAndView collectAreaAirEdit(HttpServletRequest request,
                                           HttpSession session, ModelMap modelMap, Integer collectAreaId) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (collectAreaId != null && collectAreaId != 0) {
            CollectArea collectArea = novaCollectAreaService
                    .getById(collectAreaId);
            modelMap.put("collectArea", collectArea);
            List<CollectAreaItemAir> collectAreaItemAirs = collectAreaItemAirService
                    .findByHeadId(collectArea.getId());
            modelMap.put("collectAreaItemAirs", collectAreaItemAirs);
        }

        // 获取所有的网关
        List<String> gateways = novaCollectAreaService.getAllGateWay();
        modelMap.put("gatewayInfo", JSON.toJSONString(gateways));

        // 所有客户
        modelMap.put("companyList", novaCustomerInfoService
                .findCustomerInfoByLogin(login.getUsername()));
        // 所有空调
        List<DeviceAir> deviceAirList = deviceAirService.findAll();
        modelMap.put("deviceAirInfo", JSON
                .toJSONString(deviceAirService.findCodeByList(deviceAirList)));
        return new ModelAndView("system/collectAreaAirEdit", modelMap);
    }


    /**
     * 添加collectArea
     */
    @RequestMapping(value = "saveCollectAreaAir.shtml")
    public ModelAndView saveCollectAreaAir(CollectArea collectArea,
                                        BindingResult result, HttpServletRequest request,
                                        HttpSession session, ModelMap modelMap) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (collectArea.getId() != null && collectArea.getId() != 0) {
            collectArea.setModifyby(login.getUsername());
            collectArea.setModifydate(DateUtils.getCurrentDate());
            collectArea.setModifystamp(DateUtils.getCurrentDate());
            novaCollectAreaService.updateAir(collectArea,
                    collectArea.getCollectAreaItemAirs());
        } else {
            if (collectArea.getCustomerinfoid() != null
                    && collectArea.getCustomerinfoid() != 0) {
                CustomerInfo customerInfo = novaCustomerInfoService
                        .getById(collectArea.getCustomerinfoid());
                collectArea.setCompanycode(customerInfo.getCompanycode());
            }
            //设置添加采集区域的设备类型
            DeviceType deviceType = deviceTypeService.getByName(ModelConstant.DEVICE_AIR);
            collectArea.setDevicetype(deviceType.getId());
            collectArea.setAddby(login.getUsername());
            collectArea.setAdddate(DateUtils.getCurrentDate());
            novaCollectAreaService.insertAir(collectArea,
                    collectArea.getCollectAreaItemAirs());
        }
        return collectAreaAirList(request, session, 1, new CollectArea(), modelMap);
    }

    @RequestMapping(value = "deleteCollectArea.shtml")
    public ModelAndView deletecollectArea(Integer id,
                                          HttpServletRequest request, HttpSession session, ModelMap modelMap) {
        novaCollectAreaService.delete(id);
        return collectAreaAirList(request, session, 1, new CollectArea(), modelMap);
    }
}
